package com.yoomiito.app.ui.yijiayou

import cn.droidlover.xdroidmvp.net.ApiException
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.XApi
import com.yoomiito.app.base.AppConfig
import com.yoomiito.app.base.BaseActPresenter
import com.yoomiito.app.model.LatLng
import com.yoomiito.app.model.oil.OilStationData
import com.yoomiito.app.net.YmApi
import com.yoomiito.app.showToast
import com.yoomiito.app.utils.GPSConverterUtils

class GasStationSearchPresenter(appConfig: AppConfig) : BaseActPresenter<GasStationSearchActivity>(appConfig) {

    fun searchGasStation(oilNum: Int, latlng: LatLng, type: String, gasName: String, page: Int) {
        val gps = GPSConverterUtils.gcj_To_Gps84(latlng.lat, latlng.lng)
        YmApi.getYmService()
                .oilStationList(oilNum, gps.lon, gps.lat, page, type, gasName)
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object : ApiSubscriber<List<OilStationData>>(handleException()) {
                    override fun onSuccess(t: List<OilStationData>?) {
                        v.setData(t as MutableList<OilStationData>?)
                    }

                    override fun onFail(error: ApiException?) {
                        v.showError()
                        v.dismissLoading()
                    }

                })
    }
}